﻿using System;
using System.Web;
using System.Net.Sockets;
using System.IO;
using System.Security.Cryptography;

namespace HOBA.SMSSender
{
    public static class OtherUtility
    {
        public static string GetResponse(string url)
        {
            Socket mysocket = null;
            TcpClient tcpClient;
            //StreamWriter streamWriter = null;
            StreamReader streamReader = null;
            NetworkStream networkStream = null;
            String result = "";

            try
            {
                tcpClient = new TcpClient();
                tcpClient.Connect(url, 80);

                mysocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                mysocket.Connect(url, 80);

                networkStream = tcpClient.GetStream();

                streamReader = new StreamReader(networkStream);
                //streamWriter = new StreamWriter(networkStream);
                //streamWriter.WriteLine(Header);
                //streamWriter.Flush();
                result = streamReader.ReadToEnd();

                result = "<?xml version='1.0' encoding='UTF-8'?>" + result;
                return result;
            }
            catch (Exception exc)
            {
                throw exc;
            }
        }

        public static string GetResponse2(string url, string service, string body)
        {
            int contentLength = body.Length + 2;

            //String header = "POST " + service + " HTTP/1.1" + Environment.NewLine;
            //header += "Host: " + url + Environment.NewLine;
            //header += "Content-Type: application/x-www-form-urlencoded" + Environment.NewLine;
            //header += "Content-Length: " + contentLength + Environment.NewLine;
            //header += body; --> Bad request (Invalid header name)

            //String header = "POST " + service + " HTTP/1.1 ";
            //header += "Host: " + url + " ";
            //header += "Content-Type: application/x-www-form-urlencoded ";
            //header += "Content-Length: " + contentLength + " ";
            //header += body; --> Bad request

            //String header = "POST " + service + " HTTP/1.1\n";
            //header += "Host: " + url + "\n";
            //header += "Content-Type: application/x-www-form-urlencoded\n";
            //header += "Content-Length: " + contentLength + "\n";
            //header += body;

            String header = "POST " + service + " HTTP/1.1\n";
            header += "Host: " + url + "\n";
            header += "Content-Type: text/html\n";
            header += "Content-Length: " + contentLength + "\n";
            header += body;

            Socket mysocket = null;
            TcpClient tcpClient;
            StreamWriter streamWriter = null;
            StreamReader streamReader = null;
            NetworkStream networkStream = null;

            String result = "";
            try
            {
                tcpClient = new TcpClient();
                tcpClient.Connect(url, 80);

                mysocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                mysocket.Connect(url, 80);

                networkStream = tcpClient.GetStream();

                streamReader = new StreamReader(networkStream);
                streamWriter = new StreamWriter(networkStream);
                streamWriter.WriteLine(header);
                streamWriter.Flush();

                result = streamReader.ReadToEnd();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

            return header + Environment.NewLine + result;
        }
    }
}